package com.pengji.rcp.plugin.pingyin;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Pingyin {

	protected Shell shell;
	private Text text;
	private Text text1;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Pingyin window = new Pingyin();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		shell.setLayout(new FormLayout());
		
		text = new Text(shell, SWT.BORDER);
		FormData fd_text = new FormData();
		fd_text.bottom = new FormAttachment(0, 45);
		fd_text.top = new FormAttachment(0, 22);
		fd_text.right = new FormAttachment(100, -115);
		text.setLayoutData(fd_text);
		
		text1 = new Text(shell, SWT.BORDER);
		fd_text.left = new FormAttachment(text1, 0, SWT.LEFT);
		FormData fd_text_1 = new FormData();
		fd_text_1.bottom = new FormAttachment(100, -46);
		fd_text_1.top = new FormAttachment(text, 49);
		fd_text_1.left = new FormAttachment(0, 60);
		fd_text_1.right = new FormAttachment(100, -115);
		text1.setLayoutData(fd_text_1);
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Display.getDefault().syncExec(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						//System.out.println(TestPingyin.main(text.getText()));
						text1.setText(TestPingyin.main(text.getText()));
					}
				});
			}
		});
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.top = new FormAttachment(0, 66);
		fd_btnNewButton.right = new FormAttachment(100, -22);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("New Button");

	}
}
